package com.kvn.idpt_usage.service.impl;

import com.kvn.idpt.IdptTransactional;
import com.kvn.idpt_usage.dao.IJobInfoDao;
import com.kvn.idpt_usage.entity.JobInfo;
import com.kvn.idpt_usage.service.JobInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by wangzhiyuan on 2019/1/9
 */
@Slf4j
@Service
public class JobInfoServiceImpl implements JobInfoService {
    @Resource
    IJobInfoDao jobInfoDao;

    @Override
    public JobInfo selectOne(String jobCode) {
        return jobInfoDao.selectOne(jobCode);
    }

    @IdptTransactional(biz = "addJob1", key = "adfasd")
    @Transactional
    @Override
    public void add() {
        log.info("------------------add");
        jobInfoDao.add(new JobInfo().setCode("111").setCron("111").setDescription("111").setGlobalSingle(true).setCreateTime(new Date()));
//        throw new RuntimeException("异常啦！");
    }

    //    @IdptTransactional(key = "#jobCode")
    @IdptTransactional(biz = "addJob2", key = "#jobCode.concat('xxx')")
    @Transactional
    @Override
    public void add(String jobCode) {
        log.info("------------------add, jobCode={}", jobCode);
        jobInfoDao.add(new JobInfo().setCode(jobCode).setCron("222").setDescription("222").setGlobalSingle(true).setCreateTime(new Date()));
//        throw new RuntimeException("异常啦！");
    }

    @IdptTransactional(biz = "addJob3", key = "#jobInfo.code")
    @Transactional
    @Override
    public void add(JobInfo jobInfo) {
        log.info("------------------add, jobInfo={}", jobInfo);
        jobInfoDao.add(jobInfo);
        throw new RuntimeException("异常啦！");
    }

    @IdptTransactional(biz = "addJob4", key = "#jobInfo.code")
    @Transactional
    @Override
    public JobInfo addAndReturn(JobInfo jobInfo) {
        log.info("------------------addAndReturn, jobInfo={}", jobInfo);
        jobInfoDao.add(jobInfo);
//        return jobInfo;
        throw new RuntimeException("异常啦！");
    }


}
